package cn.com.hhrcw.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.ReflectUtil;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: recruit_job
 * @description:
 * @author: JD
 * @create: 2020-07-25 10:10
 **/
@Slf4j
public class DateUtil {
    private DateUtil() {}

    private static Date getDateAdd(int days) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -days);
        return c.getTime();
    }

    public static List<String> getDaysBetwwen(int days) { //最近几天日期
        List<String> dayss = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(getDateAdd(days));
        Long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end.setTime(new Date());
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24L;
        Long time = startTIme;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
            dayss.add(df.format(d));
            time += oneDay;
        }
        Collections.reverse(dayss);
        return dayss;
    }

    @Data
    @Builder
    public static class DayCompare {
        private int year;
        private int month;
        private int day;
    }

    /**
     * 计算2个日期之间相差的  相差多少年月日
     * 比如：2011-02-02 到  2017-03-02 相差 6年，1个月，0天
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static DayCompare dayComparePrecise(Date fromDate, Date toDate) {
        Calendar from = Calendar.getInstance();
        from.setTime(fromDate);
        Calendar to = Calendar.getInstance();
        to.setTime(toDate);

        int fromYear = from.get(Calendar.YEAR);
        int fromMonth = from.get(Calendar.MONTH);
        int fromDay = from.get(Calendar.DAY_OF_MONTH);

        int toYear = to.get(Calendar.YEAR);
        int toMonth = to.get(Calendar.MONTH);
        int toDay = to.get(Calendar.DAY_OF_MONTH);
        int year = toYear - fromYear;
        int month = toMonth - fromMonth;
        int day = toDay - fromDay;
        return DayCompare.builder().day(day).month(month).year(year).build();
    }

    /**
     * 计算2个日期相差多少年
     * 列：2011-02-02  ~  2017-03-02 大约相差 6.1 年
     *
     * @param fromDate
     * @param toDate
     * @return
     */
    public static String yearCompare(Date fromDate, Date toDate) {
        DayCompare result = dayComparePrecise(fromDate, toDate);
        double month = result.getMonth();
        double year = result.getYear();
        //返回2位小数，并且四舍五入
        DecimalFormat df = new DecimalFormat("######0");
        return df.format(year + month / 12);
    }

    /**
     * 获取当前时间前size个月的时间
     *
     * @param size
     * @return
     */
    public static Date getLastMonths(int size) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.setTime(new Date());
        c.add(Calendar.MONTH, -size);
        return c.getTime();
    }

    /**
     * 获取基于当前day天前的时间 字符串形式
     *
     * @return
     */
    public static String getDayString(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTimeInMillis(System.currentTimeMillis() - day * 86400000);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(time);
    }

    public static int daysBetween(Date start, Date end) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(start);
        long time1 = cal.getTimeInMillis();
        cal.setTime(end);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(betweenDays));
    }

    public static boolean isThisYear(Object item) {
        if (item != null) {
            try {
                Method method = item.getClass().getDeclaredMethod("getCreateTime");
                if (method == null) {
                    return false;
                }
                Date date = ReflectUtil.invoke(item, method);
                return isThisYear(date);
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getMessage());
                return false;
            }
        }
        return false;
    }

    public static boolean isThisYear(Date date) {
        Date thisYear = getCurrYearFirstTime();
        return date != null && date.compareTo(thisYear) > -1;
    }

    /**
     * @param a
     * @return 获取基于当年年-a的年分
     */
    public static int getSysYear(int a) {
        Calendar date = Calendar.getInstance();
        Calendar currCal = Calendar.getInstance();
        date.set(Calendar.YEAR, currCal.get(Calendar.YEAR) - a);
        return date.get(Calendar.YEAR);
    }

    /**
     * 获取基于当前年-year的最后一天天 字符串形式
     *
     * @return
     */
    public static String getYearLast(int year) {
        Calendar instance = Calendar.getInstance();
        Calendar currCal = Calendar.getInstance();
        instance.clear();
        instance.set(Calendar.YEAR, currCal.get(Calendar.YEAR) - year);
        instance.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = instance.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(currYearLast);
    }

    /**
     * 获取基于当前年-year的第一天 字符串形式
     *
     * @return
     */
    public static String getYearFirst(int year) {
        Calendar currCal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR) - year);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(time);
    }

    /**
     * 获取今年的第一天 字符串形式
     *
     * @return
     */
    public static String getCurrYearFirst() {
        Calendar currCal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(time);
    }

    /**
     * 获取今年的第一天 时间形式
     *
     * @return
     */
    public static Date getCurrYearFirstTime() {
        Calendar currCal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
        return calendar.getTime();
    }

    /**
     * 获取今天凌晨时间戳
     *
     * @return
     */
    public static Long getTodayZero() {
        long current = System.currentTimeMillis();
        return current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
    }

    public static String getTodayZeroString(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date();
        time.setTime(getTodayZero());
        return simpleDateFormat.format(time);
    }

    /**
     * 获取year年的第一天 字符串形式
     *
     * @return
     */
    public static String getCurrYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(time);
    }

    /**
     * 获取year年的最后一天天 字符串形式
     *
     * @return Date
     */
    public static String getCurrYearLast(int year) {
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.set(Calendar.YEAR, year);
        instance.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = instance.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(currYearLast);
    }


    /**
     * 获取今年最后一天日期 字符串形式
     *
     * @return Date
     */
    public static String getCurrYearLast() {
        Calendar calendar = Calendar.getInstance();
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        instance.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = instance.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(currYearLast);
    }

    /**
     * 获取今年最后一天日期 date形式
     *
     * @return Date
     */
    public static Date getCurrYearLastTime() {
        Calendar calendar = Calendar.getInstance();
        Calendar instance = Calendar.getInstance();
        instance.clear();
        instance.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        instance.roll(Calendar.DAY_OF_YEAR, -1);
        return instance.getTime();
    }
    public static long getTime(Date oldTime,Date newTime) {

        long nTime = newTime.getTime();
        //从对象中拿到时间
        long oTime = oldTime.getTime();
        long diff = (nTime - oTime);
        return diff / 1000;
    }
}
